#include "Misc.h"

CCNode* Misc::getChildByName(CCNode* pNode, const char* nodeName)
{
	std::string name(nodeName);
	if (pNode)
	{
		if (name.empty())
		{
			return NULL;
		}


		CCArray* pChildren = pNode->getChildren();
		if(pChildren && pChildren->count() > 0)
		{
			CCObject* child;
			CCARRAY_FOREACH(pChildren, child)
			{
				CCNode* pNode = (CCNode*) child;
				if(pNode && pNode->getName() == name)
					return pNode;
			}
			//if (bChild)
			{
				CCARRAY_FOREACH(pChildren, child)
				{
					CCNode* pNode = (CCNode*) child;
					if(pNode)
					{
						CCNode* pFind = getChildByName(pNode, nodeName);
						if (pFind)
							return pFind;
					}
				}
			}
		}
		return NULL;
	}
	else
	{
		CCLOG("%s getChildByName : %s is NULL", pNode->getName().c_str(), nodeName);
	}
	return NULL;
}

std::string Misc::getNodeName(CCNode* pNode)
{
	if (pNode)
	{
		return pNode->getName();
	}
	return "";
}

void Misc::setNodeName(CCNode* pNode, const char* name)
{
	if (pNode)
	{
		pNode->setName(name);
	}
}

void* Misc::getUserData(CCNode* pNode)
{
	return pNode->getUserData();
}

void Misc::log(const char* msg)
{
	CCLOG(msg);
}

std::string Misc::fullPathFile(const char* filename)
{
	return CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(filename);
}

bool Misc::collideNode(CCNode* node1, CCNode* node2)
{
	CCRect rect1 = CCRectMake(0, 0, node1->getContentSize().width, node1->getContentSize().height);
	rect1  = CCRectApplyAffineTransform(rect1, node1->nodeToWorldTransform());

	CCRect rect2 = CCRectMake(0, 0, node2->getContentSize().width, node2->getContentSize().height);
	rect2  = CCRectApplyAffineTransform(rect2, node2->nodeToWorldTransform());

	return CCRect::CCRectIntersectsRect(rect1, rect2);
}